package com.gitee.dupe.dict.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gitee.dolt.annotation.security.PreAuth;
import com.gitee.dolt.boot.controller.SuperCacheController;
import com.gitee.dolt.boot.controller.SuperController;
import com.gitee.dolt.boot.request.QueryParams;
import com.gitee.dolt.core.base.Result;
import com.gitee.dolt.core.base.entity.SuperEntity;
import com.gitee.dupe.dict.dto.DictionaryQueryDTO;
import com.gitee.dupe.dict.dto.DictionarySaveDTO;
import com.gitee.dupe.dict.dto.DictionaryTypeSaveDTO;
import com.gitee.dupe.dict.entity.Dictionary;
import com.gitee.dupe.dict.service.IDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LYH
 * @since 2020-10-11
 */
@RestController
@RequestMapping("/b/dictionary")
@Validated
@Api(value = "Dictionary", tags = "字典")
@PreAuth(replace = "b:dictionary:")
public class DictionaryController extends SuperCacheController<Dictionary, IDictionaryService, Long, DictionarySaveDTO, DictionarySaveDTO, DictionaryQueryDTO> {


    // 使用了缓存 无用
    @Override
    public QueryWrapper<Dictionary> handlerQueryWrapper(Dictionary dictionary, QueryParams<DictionaryQueryDTO> params) {
        QueryWrapper<Dictionary> qw = Wrappers.query();
        String keyword = params.getKeyword();
        if (StrUtil.isNotEmpty(keyword)) {
            qw.lambda().and(e -> e.like(Dictionary::getLabel, keyword).or().like(Dictionary::getKey, keyword)
                    .or().like(Dictionary::getType, keyword));
        }
        DictionaryQueryDTO model = params.getModel();
        Long parentId = model.getParentId();
        if (parentId == null) {
            qw.lambda().isNull(Dictionary::getParentId);
        } else {
            qw.lambda().eq(Dictionary::getParentId, parentId);
        }
        return qw;
    }

    @Override
    public Result handlerSave(DictionarySaveDTO model) {
        return Result.ok(baseService.handlerSave(model));
    }

    @Override
    public Result handlerUpdate(DictionarySaveDTO model) {
        return Result.ok(baseService.handlerUpdate(model));
    }

    @Override
    public boolean enableQuery() {
        return true;
    }


    @ApiOperation(value = "保存-字典类型")
    @PostMapping(value = "/type")
    @PreAuth("hasAnyPermission('{}save')")
    public Result saveType(@RequestBody @Validated(SuperEntity.Save.class) DictionaryTypeSaveDTO dictType) {
        return Result.ok(baseService.saveType(dictType));
    }

    @ApiOperation(value = "修改-字典类型")
    @PutMapping(value = "/type")
    @PreAuth("hasAnyPermission('{}edit')")
    public Result updateType(@RequestBody @Validated(SuperEntity.Update.class) DictionaryTypeSaveDTO dictType) {
        return Result.ok(baseService.updateType(dictType));
    }

    @ApiOperation(value = "删除-字典类型")
    @DeleteMapping(value = "/type")
    @PreAuth("hasAnyPermission('{}delete')")
    public Result deleteType(@RequestBody List<Long> ids) {
        return Result.ok(baseService.deleteType(ids));
    }

    @ApiOperation(value = "获取编码下元素")
    @GetMapping("/listByTypes")
    @PreAuth(enabled = false)
    public Result listByTypes(@RequestParam("types[]") List<String> types) {
        return Result.ok(baseService.listByTypes(types));
    }
}
